package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.demo.entity.OrderBase;
import com.example.demo.entity.OrderImg;
import com.example.demo.entity.OrderQuery;
import com.example.demo.service.OrderService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    // 通过ID查找客户
    @GetMapping("/orderFind")
    public Map<String,Object> orderFind(@RequestParam("id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        OrderBase orderBase = orderService.getOrder(id);
        dataMap.put("status","0");
        dataMap.put("data",orderBase);
        return dataMap;
    }

    @GetMapping("/order")
    public  Map insertOrder(@RequestParam Map order, HttpServletRequest request, HttpSession session){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        OrderBase save = orderService.insertOrder(order,request);

        dataMap.put("status",0);
        dataMap.put("message","保存成功！");
        return dataMap;
    }

    @GetMapping("/orderAll")
    public Map<String,Object> orderAll(@RequestParam Integer  page,@RequestParam Integer  currentPage){
        Map<String,Object> dataMap = new HashMap<String,Object>();

        List<OrderBase> order = orderService.orderAll();
        // 这里根据当前页数以及所显示的条数进行对应的数据查询

        // 这个是通过更新人进行查询数据
        Long orderCount = orderService.countAllByUpdater("1");
        Map<String,Object> pageArr = new HashMap<String,Object>();
        pageArr.put("pageCount",orderCount);
        pageArr.put("currentPage",currentPage);
        pageArr.put("pagesize",page);

        dataMap.put("status",0);
        dataMap.put("data",order);
        dataMap.put("pageInfo",pageArr);
        return dataMap;
    }

    /*
    *  这是分页查询订单数量
    *   parms => page:第一个页
    *            size:要几条
    *            order:订单里面的参数集合(id.orderName)
    * */
    @GetMapping("/orderQuery")
    public Map<String,Object> findBookQuery(@RequestParam Map orderQuery,HttpSession session){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        JSONObject orderJson = (JSONObject) JSONObject.parse(orderQuery.get("orderQuery").toString());
        int page = Integer.valueOf(orderQuery.get("page").toString());
        int size = Integer.valueOf(orderQuery.get("size").toString());
        String userId = session.getAttribute("user").toString();
        Page<OrderBase> datas = orderService.findBookCriteria(page, size,orderJson,userId);
        dataMap.put("data", JSON.parse(JSON.toJSONString(datas, SerializerFeature.DisableCircularReferenceDetect)));
        dataMap.put("status",0);
        return dataMap;
    }

    /*
    *  这是通过订单ID删除指定的订单
    *   parms => id:要删除的ID
    * */
    @GetMapping("/orderDelete")
    public Map<String,Object> orderDelete( @RequestParam(value = "id", defaultValue = "0") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null ){
            dataMap.put("errMsg","无效的ID");
            dataMap.put("status",20);
        }else{
            Boolean dataBool = orderService.orderDelete(id);
            dataMap.put("data", dataBool);
            dataMap.put("status",0);
        }
        return dataMap;
    }

    /*
    *  确认收款
    *   parms => id:订单ID
    * */
    @PostMapping("/orderPay")
    public Map<String,Object> orderPay(@RequestParam(value = "id") Integer id,@RequestParam(value = "payBank") String payBank){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null || StringUtils.isEmpty(payBank)){
            dataMap.put("message","参数不对！");
            dataMap.put("status",20);
        }else{
            orderService.orderPay(id,payBank);
            dataMap.put("message","收款成功！");
            dataMap.put("status",0);
        }
        return dataMap;
    }

    /*
    *  确认发货
    *   parms => id:订单ID
    * */
    @PostMapping("/orderSend")
    public Map<String,Object> orderSend(@RequestParam(value = "id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null){
            dataMap.put("message","参数不对！");
            dataMap.put("status",20);
        }else{
            orderService.orderSend(id);
            dataMap.put("message","发货成功！");
            dataMap.put("status",0);
        }
        return dataMap;
    }

    /*
   *  确认收货
   *   parms => id:订单ID
   * */
    @PostMapping("/orderDone")
    public Map<String,Object> orderDone(@RequestParam(value = "id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null){
            dataMap.put("message","参数不对！");
            dataMap.put("status",20);
        }else{
            orderService.orderDone(id);
            dataMap.put("message","收货成功！");
            dataMap.put("status",0);
        }
        return dataMap;
    }








}
